package com.bjypc.youth.web.chart;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionSupport;

public class ParttimeLeagueCadreInfo2007Action extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 2883083919383011857L;
	// JChartResult->ActionInvocation.getStack().findValue("chart")
	private JFreeChart chart;
	private Logger logger = Logger.getLogger(getClass());

	public ParttimeLeagueCadreInfo2007Action() {
		super();
	}

	private CategoryDataset createDataset() {
		DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
		defaultcategorydataset.addValue(2978L, "党员", "党员比例");
		defaultcategorydataset.addValue(265L, "同级党委成员", "党员比例");
		defaultcategorydataset.addValue(2349L, "其它", "党员比例");
		defaultcategorydataset.addValue(2902L, "男干部", "性别比例");
		defaultcategorydataset.addValue(2690L, "女干部", "性别比例");
		defaultcategorydataset.addValue(5308L, "汉族", "民族比例");
		defaultcategorydataset.addValue(284L, "少数民族", "民族比例");
		defaultcategorydataset.addValue(4826L, "大学以上学历", "文化结构");
		defaultcategorydataset.addValue(766L, "其它", "文化结构");
		return defaultcategorydataset;
	}

	@Override
	public String execute() throws Exception {

		CategoryDataset categorydataset = createDataset();

		this.chart = ChartFactory.createStackedBarChart("2007年度北京市共青团兼职团干部情况",
				"干部情况", "数据", categorydataset, PlotOrientation.VERTICAL, true,
				true, false);
		this.chart.setBackgroundPaint(Color.white);
		CategoryPlot categoryplot = (CategoryPlot) this.chart.getPlot();

		String path = ServletActionContext.getServletContext().getRealPath(
				"/images/bjyouthleague.jpg");
		logger.info("Background image path:" + path);
		if (path != null && path.length() > 0) {
			ImageIcon imageicon = new ImageIcon(path);
			categoryplot.setBackgroundImage(imageicon.getImage());
			categoryplot.setBackgroundPaint(null);
		} else {
			categoryplot.setBackgroundPaint(Color.lightGray);
		}
		categoryplot.setRangeGridlinePaint(Color.white);
		NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
		numberaxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
		StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot
				.getRenderer();
		stackedbarrenderer.setRenderAsPercentages(true);
		stackedbarrenderer.setDrawBarOutline(false);
		stackedbarrenderer.setBaseItemLabelsVisible(true);
		stackedbarrenderer
				.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(
						"{3}", NumberFormat.getIntegerInstance(),
						new DecimalFormat("0.0%")));

		return SUCCESS;
	}

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}
}
